package com.liy.cloud.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  功能描述：IP 验证
 */
public class IPValidator {

    /**
     *
     * 功能描述：ip地址合法校验
     * <p>1.25[0-5]             250-255</p>
     * <p>2.2[0-4]\\d           200-249</p>
     * <p>3.1\\d{1}\\d{1}       100-199</p>
     * <p>4.[1-9]{1}\\d{1}      10-99</p>
     * <p>5.\\d{1}              0-9</p>
     * <p>6.($|(?!\\.$)\\.)     结束 或者 不以.结束的加上.</p>
     * @param ip
     * @return ip合法返回true，不合法返回false
     * @auther libs
     * @date 2018/8/16 10:40
     */
    public static boolean validate(String ip){
        Pattern p = Pattern.compile("^((25[0-5]|2[0-4]\\d|1\\d{1}\\d{1}|[1-9]{1}\\d{1}|\\d{1})($|(?!\\.$)\\.)){4}$");
        Matcher m = p.matcher(ip);
        return m.matches();
    }


    public static void main(String[] args){
        System.out.println(validate("0.0.0.0"));
        System.out.println(validate("255.255.255.255"));
        System.out.println(validate("10.146.38.12"));
        System.out.println(validate("00.146.38.12"));
        System.out.println(validate("256.255.255.255"));
        System.out.println(validate("10.146.38.12."));
        System.out.println(validate("10.146.38..12"));
        System.out.println(validate(".10.146.38.12"));
        System.out.println(validate("10.146.38"));
        System.out.println(validate("10.146.3812"));
        System.out.println(validate("10.146.38.12.12"));
    }
}
